package jp.sourceforge.akjrcp.dropsomething2;

import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Text;

import jp.sourceforge.akjrcp.akj_betas.Messages;
import jp.sourceforge.akjrcp.commons.ui.widget.WidgetBuilder;
import jp.sourceforge.akjrcp.ui.GenericParametersTableViewer;

public class SearchBox extends Composite{
	GenericParametersTableViewer target;
	AbstractFFMpegDropSomething2 tableOwner;
	private Text text;
	public SearchBox(Composite parent, int style) {
		super(parent, style);
	//	this.target=target;
		setLayout(new GridLayout(3,false));
		text = new Text(this,SWT.BORDER);
		text.addSelectionListener(new SelectionAdapter() {
			public void widgetDefaultSelected(SelectionEvent e) {
				doDownSearch();
			}
		});
		text.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		final Button down=WidgetBuilder.createButton(this, Messages.getString("searchbox.down"));
		down.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				doDownSearch();
			}
		});
		final Button up=WidgetBuilder.createButton(this, Messages.getString("searchbox.up"));
		up.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				String word=text.getText();
				int start=target.getCurrentSelectionIndex()-1;
				if(start<0){
					start=target.getTable().getItemCount()-1;//start with last one
				}
				int index=target.find(word, true, true, Math.max(0, start), false);
				if(index!=-1){
					target.getTable().setSelection(index);
					tableOwner.tableSelectionChanged();
					}
			}
		});
	}
	public void doDownSearch(){
		String word=text.getText();
		int start= target.getCurrentSelectionIndex()+1;
		int index=target.find(word, true, true, Math.max(0,start), true);
		if(index!=-1){
		target.getTable().setSelection(index);
		tableOwner.tableSelectionChanged();
		}
	}
	public GenericParametersTableViewer getTarget() {
		return target;
	}
	public void setTarget(GenericParametersTableViewer target) {
		this.target = target;
	}
	public AbstractFFMpegDropSomething2 getTableOwner() {
		return tableOwner;
	}
	public void setTableOwner(AbstractFFMpegDropSomething2 tableOwner) {
		this.tableOwner = tableOwner;
	}

}
